/**
 * Created by Delicate on 2017/5/31.
 */
var fairList;
$(document).ready(function(){
	init();
    monitor();
    service();
    page();
})

function init(){
    fairList = [];
    $(".box-num-panel").text($(".u-row-cell-item").length);
    load(1);
    
    $("#jumpVal").keydown(function(e) {  
        if (e.keyCode == 13) {  
              jump();
        }  
   });
//    $(window).bind('beforeunload', function(){ 
//  	  return '确认离开此页吗'; 
//  	}); 
}


function monitor(){
    window.onscroll = function () {
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        if (t > 280) {
            $(".c-article").addClass("g-info-hide");
        } else {
            $(".c-article").removeClass("g-info-hide");
        }
    }
    $(".u-row-box-hide").click(res=>{
        if($(".c-article").hasClass("g-info-hide"))$(".c-article").removeClass("g-info-hide");
    });
    $(".m-select-search .task-next").click(res=>{
        document.documentElement.scrollTop = 0;
        document.body.scrollTop = 0;
        load(1);
    });
}
function service(){

}

function delFair(obj,callback){
    fairList.splice($.inArray(obj.id,fairList),1);
    $("#fair_"+obj.id).remove();
    $(".box-num-panel").text($(".u-row-cell-item").length);
    callback();
}

function addFair(obj,setNum,callback){
	
    if(fairList.length<setNum){
        fairList.push(obj.id);
        var _html = '<li ' +
            'data-id="'+obj.id+'" ' +
            'id="fair_'+obj.id+'" ' +
            'title="'+obj.name+'" ' +
            'style="background:url('+obj.image+') center/cover no-repeat" ' +
            'class="u-row-cell-item"></li>';
        $(".u-row-cell-list").append(_html);
        $(".u-row-cell-item").unbind('click').click(function(){
            $(this).remove();
            var id = $(this).data("id");
            fairList.splice($.inArray(id,fairList),1);
            fitlerFair(id);
            $(".box-num-panel").text($(".u-row-cell-item").length);
        });
        $(".box-num-panel").text($(".u-row-cell-item").length);
        callback();
    }else{
    	layer.alert("这次任务最多只能选择"+setNum+"名主播");
    }
}

function fitlerFair(id){
    $(".m-choice-list").find('li[data-id="'+id+'"]').removeClass("m-choice-cell-active");
}

function initFair(){
    if(fairList.length>0){
        for(var id of fairList){
            $(".m-choice-list").find('li[data-id="'+id+'"]').addClass("m-choice-cell-active");
        }
    }
}
var totalPage=1;
function load(number){
    $.ajax({
        type:"post",
        url:"/platform/fair/list",
        datatype:"json",
        data:{
            pageNum: number,
            pageSize: 12,
            nickName: $("#nickName").val(),
            channel: $("#channel").val(),
            city: $("#city").val(),
            province: $("#province").val(),
            platform: $("#platform").val(),
            feature: $("#feature").val(),
            price: $("#price").val()
        },
        success:function(data){
            let _html = "";
            if(data.list.length>0){
                for (var i=0;i<data.list.length;i++){
                    var item = data.list[i];
                    _html += '<li class="m-choice-cell" style="height:215px;" onclick="cellClick(this)"  data-id="'+item.id+'" data-head="'+item.photo+'" data-name="'+item.nick_name+'"> ' +
                        '<div class="u-cell-item-head"> ' +
                        '<div class="u-item-head" style="background: url('+item.photo+') center/cover no-repeat"></div> ' +
                        '<div class="u-item-name" style="font-size:16px;margin-top:10px;">'+item.nick_name+'</div> ' +
                        '<div class="u-item-summary">'+item.attrString+'</div> ' +
                        '</div> ' +
                        '<div class="u-cell-item-number"> ' +
                        '<div class="u-item-yxl"> ' +
                        '<div class="u-item-icon"></div> ' +
                        '<div class="u-item-text">点击数'+item.click+'</div> ' +
                        '</div> ' +
                        '<div class="u-item-rate"> ' +
                        '<div class="u-item-icon" style="background: url(/assets/front/web/resources/icon-zh.png) center/contain no-repeat;"></div> ' +
                        '<div class="u-item-text">转化数'+item.cvs+'</div> ' +
                        '</div> ' +
                        '</div> ' +
                        '</li>';
                }
                initFair();
                page(data.totalPage,data.pageNumber);
//                $("#m_choice_page").show();
                if(data.totalPage>1){
                	$("#m_choice_page_container").show();
                }else{
                	$("#m_choice_page_container").hide();
                }
            }else{
            	_html+='<div style="margin:0 auto;padding:15px;color:red">暂时没有相关数据信息</div>'
            	$("#m_choice_page_container").hide();
            }
            $(".m-choice-list").html(_html);
        }
    })
}

function page(total,index){
	totalPage =total;
    laypage({
        cont: 'm_choice_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: total?total:1, //通过后台拿到的总页数
        curr: index||1, //当前页
        jump: function(obj, first){ //触发分页后的回调
            if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                load(obj.curr);
            }
        }
    });
}

function jump(){
	var jumpVal=$("#jumpVal").val();
	var re = /^[0-9]+.?[0-9]*$/;
	if (!re.test(jumpVal)) {
		layer.alert("请输入正确的整数");
        return;
	}else if(jumpVal<=0){
		layer.alert("请输入大于1的整数");
        return;
	}else if(jumpVal>totalPage){
		layer.alert("已经超出最大页码数了");
        return;
	}else if(jumpVal&&jumpVal<=totalPage){
		load(jumpVal);
	}
}

function cellClick(obj){
	var setNum = $(".m-choice-list").attr("data-num");
    var _data = {};
    _data.id = $(obj).data("id");
    _data.image = $(obj).data("head");
    _data.name = $(obj).data("name");
    if($(obj).hasClass("m-choice-cell-active")){
        delFair(_data,function(){
            $(obj).removeClass("m-choice-cell-active");
        })
    }else{
        addFair(_data,setNum,function(){
            $(obj).addClass("m-choice-cell-active");
        })
    }
}

//选择省 生成对应市
function changeRegion(obj){
    var val=$(obj).val();
    if(val==""){
    	$("#city").empty();
        $("#city").append("<option value=''>请选择</option>")
        return;
    }
    $.ajax({
        type:"post",
        url:"/platform/fair/region",
        datatype:"json",
        data:{"id":val},
        success:function(data){
            if(data.length>0){
                $("#city").empty();
                $("#city").append("<option value=''>请选择</option>")
                for (var i=0;i<data.length;i++){
                    $("#city").append("<option value="+data[i].REGION_ID+" class=''>"+data[i].REGION_NAME+"</option>")
                }
            }else{

            }
        }
    })

}


function create(){
	var setNum = $(".m-choice-list").attr("data-num");
    if(fairList.length==0){
        layer.alert("请选择主播");
        return;
    }
    
    if(fairList.length>setNum){
        layer.alert("指定主播，最多输入10位网红");
        return;
    }
    $("input[name='fairs']").val(fairList);
    $("#fairForm").submit();
}


function clearSearch(){
	$("#nickName").val("");
}
